// pages/distribution/distribution.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yidong: [
      {
        urls: "../../images/banner1.jpg",
        name: "需使用4G飞享套餐。赠送12GB流量，2400分钟通话",
        price: "1370",
        goods_id: "1",
        yongjin:'10',
        type:1
      },
    ],
  },
  fenxiao: function () {
    this.setData({
      fenxiao: true
    })
  },
  index:function(){
    app.swtTa('../index/index')
  },
  guanbi: function () {
    this.setData({
      fenxiao: false
    });
  },
  onShareAppMessage: function (res) {
    var uid = wx.getStorageSync('uid');
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '自定义转发标题',
      path: '/pages/index/index?uid=' + uid,
      success: function (res) {
        console.log('-----',res)
        // 转发成功
        wx.showShareMenu({
          // 要求小程序返回分享目标信息
          withShareTicket: true
        });
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  /**
   * 打开分享面板
   */
  openSharePosterPanel: function () {
    var that = this;
    that.setData({
      sharePosterPanel: true,
      fenxiao: false
    });
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 600,
      height: 800,
      destWidth: 600,
      destHeight: 800,
      canvasId: 'myCanvas',
      success: function (res) {
        console.log('---', res.tempFilePath);
        that.setData({
          shareImgSrc: res.tempFilePath
        })

      },
      fail: function (res) {
        console.log('-----', res)
      }
    }, this)
  },
  /**
   * 关闭分享面板
   */
  closeSharePosterPanel: function () {
    this.setData({
      sharePosterPanel: false
    });
  },
  saveImage:function(){
    //4. 当用户点击分享到朋友圈时，将图片保存到相册
    var that = this;
    wx.saveImageToPhotosAlbum({
      filePath: that.data.shareImgSrc,
      success(res) {
        console.log('----',res)
        wx.showModal({
          title: '存图成功',
          content: '图片成功保存到相册了，去发圈噻~',
          showCancel: false,
          confirmText: '好哒',
          confirmColor: '#72B9C3',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
            }
            // that.hideShareImg()
          }
        })
      }
    })
  },
  qrcode: function () {
    var that = this;
    wx.request({
      url: 'test.php', //仅为示例，并非真实的接口地址
      data: {
        x: '',
        y: ''
      },
      success: function (res) {
        console.log(res.data)
      }
    })
  },
  onLoad:function(){
    var that = this;
    wx.request({
      url: app.appUrl + '/User/erweima',
      data: {
        uid: wx.getStorageSync('uid')
      },
      success: function (e) {
        console.log('====', e);
        if (e.data.code == 1) {
          const ctx = wx.createCanvasContext('myCanvas');
          var imgPath = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529400923956&di=747cb69dc59ae305bc359cfc94121c44&imgtype=0&src=http%3A%2F%2Fimg0.utuku.china.com%2Fuploadimg%2Fgame%2F20161222%2F2d5e5c53-56f1-4378-819d-981bdeb20214.jpg'
          var bgImgPath = e.data.imgUrl;
          ctx.drawImage(imgPath, 0, 0, 375, 600);
          ctx.setFillStyle('red')
          ctx.drawImage(bgImgPath, 175, 387, 200, 213);
          ctx.setFontSize(20)
          ctx.fillText('长按扫码查看详情', 200, 320)
          ctx.draw();
        }
      }
    })
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowWidth)
        console.log(res.windowHeight)
      }
    })
  },
  onShow: function () {
    var that = this;
    wx.request({
      url: app.appUrl + '/Goods/fxGoodsList',
      data: {
        bid: wx.getStorageSync('bid')
      },
      success: function (e) {
        console.log('商品列表', e.data.list);
        that.setData({
          yidong: e.data.list
        })
      }
    })
  },
})